package org.jeecg.common.modules.dic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.modules.dic.vo.GoodAtChildren;
import org.jeecg.common.modules.dic.vo.GoodAtResVo;
import org.jeecg.common.modules.dic.vo.GoodResponseVo;
import org.jeecg.common.modules.dic.entity.DicBeGoodAt;
import org.jeecg.common.modules.dic.mapper.DicBeGoodAtMapper;
import org.jeecg.common.modules.dic.service.IDicBeGoodAtService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: dic_be_good_at
 * @Author: jeecg-boot
 * @Date:   2020-08-27
 * @Version: V1.0
 */
@Service
public class DicBeGoodAtServiceImpl extends ServiceImpl<DicBeGoodAtMapper, DicBeGoodAt> implements IDicBeGoodAtService {

    private IDicBeGoodAtService beGoodAtService;

    @Override
    public List<DicBeGoodAt> getDicBeGoodInfo(){
        LambdaQueryWrapper<DicBeGoodAt> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DicBeGoodAt::getParentId,0);
        queryWrapper.eq(DicBeGoodAt::getInvaild,0);
        return list(queryWrapper);
    }

    @Override
    public List<GoodAtResVo> getGoodAll(){
//        LambdaQueryWrapper<DicBeGoodAt> query = Wrappers.lambdaQuery();
//        query.eq(DicBeGoodAt::getParentId,0);
//        query.orderByDesc(DicBeGoodAt::getSort);
//        List<DicBeGoodAt> goods = list(query);
//        List<GoodResponseVo> vos = new ArrayList<>();
//        for (DicBeGoodAt good:goods) {
//            LambdaQueryWrapper<DicBeGoodAt> queryWrapper = Wrappers.lambdaQuery();
//            queryWrapper.eq(DicBeGoodAt::getParentId,good.getId());
//            queryWrapper.orderByDesc(DicBeGoodAt::getSort);
//            GoodResponseVo vo = new GoodResponseVo();
//            vo.setId(good.getId());
//            vo.setName(good.getName());
//            vo.setImg(good.getImg());
//            vo.setSort(good.getSort());
//            vo.setList(list(queryWrapper));
//            vos.add(vo);
//        }
//        return vos;

        LambdaQueryWrapper<DicBeGoodAt> query = Wrappers.lambdaQuery();
        query.eq(DicBeGoodAt::getParentId,0);
        query.orderByDesc(DicBeGoodAt::getSort);
        List<DicBeGoodAt> goods = list(query);

        List<GoodAtResVo> vos = new ArrayList<>();
        for (DicBeGoodAt good:goods) {
            LambdaQueryWrapper<DicBeGoodAt> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DicBeGoodAt::getParentId,good.getId());
            queryWrapper.orderByDesc(DicBeGoodAt::getSort);
            GoodAtResVo vo = new GoodAtResVo();
            vo.setValue(good.getId());
            vo.setLabel(good.getName());

            //处理擅长子级
            List<DicBeGoodAt> goodAtList = list(queryWrapper);
            List goodAtChildrenList = new ArrayList();
            for(DicBeGoodAt beGoodAt : goodAtList){
                GoodAtChildren goodAtChildren = this.initGoodAtChildren(beGoodAt);
                goodAtChildrenList.add(goodAtChildren);
            }
            vo.setChildren(goodAtChildrenList);

            vos.add(vo);
        }
        return vos;
    }


    /**
     * 处理擅长子级
     * @param dicBeGoodAt
     * @return
     */
    public GoodAtChildren initGoodAtChildren(DicBeGoodAt dicBeGoodAt){
        GoodAtChildren goodAtChildren = new GoodAtChildren();
        goodAtChildren.setLabel(dicBeGoodAt.getName());
        goodAtChildren.setValue(dicBeGoodAt.getId());
        return goodAtChildren;
    }
}
